#include "mainconfigdlg.h"
#include "ui_mainconfigdlg.h"
#include "pluginbase.h"
#include "account.h"

MainConfigDlg::MainConfigDlg(uint uid, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MainConfigDlg)
{
    ui->setupUi(this);
    Account* act = AccountManager::self()->account(uid);
    QMap<QString, ConfigWidgetBase*> lst = PluginManager::self()->createConfigWidgets(act);
    lstWidget = lst;
    QMap<QString, ConfigWidgetBase*>::Iterator it = lst.begin();
    while(it != lst.end())
    {
        ConfigWidgetBase* widget = it.value();
        if(widget)
        {
            ui->tb->addTab(widget, it.key());
            widget->show();
        }
        ++it;
    }
    connect(ui->btnBox, SIGNAL(accepted()), this, SLOT(done()));
}

MainConfigDlg::~MainConfigDlg()
{
    QMap<QString, ConfigWidgetBase*>::Iterator it = lstWidget.begin();
    while(it != lstWidget.end())
    {
        ConfigWidgetBase* widget = it.value();
        delete widget;
        ++it;
    }

    delete ui;
}

void MainConfigDlg::done()
{
    QMap<QString, ConfigWidgetBase*>::Iterator it = lstWidget.begin();
    while(it != lstWidget.end())
    {
        ConfigWidgetBase* widget = it.value();
        if(widget)
            widget->applyConfigs();
        ++it;
    }
    hide();
}

void MainConfigDlg::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
